/** 
*
* @author  jiwf 
* @date 创建时间：2016年10月22日 下午6:56:33 
* @version 1.0 
* @since  
*  
*/
package com.xbstar.web;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xbstar.bean.Comment;
import com.xbstar.bean.UserOrder;
import com.xbstar.dao.CommentRepository;
import com.xbstar.dao.OrderRepository;
import com.xbstar.tools.Timer;

@RestController
@CrossOrigin
@RequestMapping("/comment")
public class CommentController {
	@Autowired
    DataSource dataSource;
	@Autowired
	CommentRepository commentRe;
	@Autowired
	OrderRepository orderRe;
	@RequestMapping("/getall")
	public String getAllComment(){
		JSONObject json=new JSONObject();
		JSONArray array=new JSONArray();
		  try {
			Connection conn  = dataSource.getConnection();
			 String sqlString="SELECT nickName,userId,routeInfoName,routeId,destinationId,orderId,content,commentLevel,id,submitTime,orderBookTime,orderCost,orderState from userroutecommentview where isOk=?";
			 PreparedStatement ps = conn.prepareStatement(sqlString);
			 ps.setString(1, "0");
			 ResultSet rs = ps.executeQuery();
			 while (rs.next()) {
				 JSONObject tjson=new JSONObject();
				 tjson.put("orderId", rs.getString("orderId"));
				 tjson.put("orderBookTime", rs.getString("orderBookTime"));
				 tjson.put("nickName", rs.getString("nickName"));
				 tjson.put("routeInfoName", rs.getString("routeInfoName"));
				 tjson.put("routeId", rs.getString("routeId"));
				 tjson.put("destinationId", rs.getString("destinationId"));
				 tjson.put("content", rs.getString("content"));
				 tjson.put("commentLevel", rs.getString("commentLevel"));
				 tjson.put("id", rs.getString("id"));
				 tjson.put("submitTime", rs.getString("submitTime"));
				 tjson.put("orderCost", rs.getString("orderCost"));
				 array.put(tjson);
			 }
			 json.put(UserPackage.STATUS, UserPackage.SUCCESS);
			 json.put(UserPackage.DATA, array);
			 rs.close();
			 ps.close();
			 conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
			json.put(UserPackage.STATUS, UserPackage.SERVER_ERROR);
		}
		 
		return json.toString();
	}
	@RequestMapping("/add")
	String addComment(@RequestParam("orderId")Long orderId, @RequestParam("content")String content,@RequestParam("level")String commentLevel){
		JSONObject json=new JSONObject();
		try {
			UserOrder order=orderRe.findOne(orderId);
			
			if(order==null){
				json.put(UserPackage.STATUS, UserPackage.EMPTY);
			}else{
				order.setState("3");//修改订单状态
				Comment comment=new Comment();
				comment.setOrderId(orderId);
				comment.setUserId(order.getUserId());
				comment.setRouteId(order.getRouteInfoId());
				comment.setContent(content);
				comment.setCommentLevel(commentLevel);
				comment.setSubmitTime(Timer.getCurrentString());
				comment.setIsOk("0");
				comment=commentRe.save(comment);
				orderRe.save(order);
				 json.put(UserPackage.STATUS, UserPackage.SUCCESS);
				 JSONObject tjson=new JSONObject();
				 tjson.put("id", comment.getId());
				 json.put(UserPackage.DATA, tjson);
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put(UserPackage.STATUS, UserPackage.SERVER_ERROR);
		}
		return json.toString();
	}
	@RequestMapping("/del")
	public String delComment(@RequestParam("id")Long id){
		JSONObject json=new JSONObject();
		try{
			commentRe.delete(id);
			json.put(UserPackage.STATUS, UserPackage.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			json.put(UserPackage.STATUS, UserPackage.SERVER_ERROR);
		}
		return json.toString();
	}
	@RequestMapping("/edit")
	public String editComment(@RequestParam("id")Long id,HttpServletRequest req){
		JSONObject json=new JSONObject();
		try{
			Comment comment= commentRe.findOne(id);
			String commentLevel=req.getParameter("commentLevel");
			String content=req.getParameter("content");
			if(comment==null&&commentLevel==null){
				json.put(UserPackage.STATUS, UserPackage.LOST_PARAM);
			}else{
				comment.setCommentLevel(commentLevel);
				comment.setContent(content);
				comment.setUpdatedTime(Timer.getCurrentString());
				commentRe.save(comment);
				json.put(UserPackage.STATUS, UserPackage.SUCCESS);	
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			json.put(UserPackage.STATUS, UserPackage.SERVER_ERROR);
		}
		return json.toString();
	}
	@RequestMapping("/editstatus")
	public String editStatus(@RequestParam("id")Long id){
		JSONObject json=new JSONObject();
		try{
				Comment comment=commentRe.findOne(id);
				comment.setIsOk("1");
				comment.setUpdatedTime(Timer.getCurrentString());
				commentRe.save(comment);
				json.put(UserPackage.STATUS, UserPackage.SUCCESS);	
		} catch (Exception e) {
			e.printStackTrace();
			json.put(UserPackage.STATUS, UserPackage.SERVER_ERROR);
		}
		return json.toString();
	}
	@RequestMapping("/getone")
	String getOneComment(@RequestParam("orderId")Long orderId){
		JSONObject json=new JSONObject();
		JSONObject tjson=new JSONObject();
		Comment comment= commentRe.findTop1ByOrderId(orderId);
		if(comment!=null){
			tjson.put("level", comment.getCommentLevel());
			tjson.put("content", comment.getContent());
			tjson.put("have", 1);
		}else{
			tjson.put("have", 0);
		}
		json.put("status", "200");
		json.put("data", tjson);
		return json.toString();
	}
}
